/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.language;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ac.jp.u_tokyo.SyncLib.util.Pair;

public class SyncDecl implements Iterable<Pair<String, Const>>
{
	List<Pair<String, Const>> _paraMap = new LinkedList<Pair<String, Const>>();
	public void addDefaultPara(String name, Const defaultValue) {
		_paraMap.add(new Pair<String, Const>(name, defaultValue));
	}
	public void addNonDefaultPara(String name) {
		_paraMap.add(new Pair<String, Const>(name, null));
	}
	
	
	SyncBody _body;
	public void setBody(SyncBody body)
	{
		this._body = body;
	}
	
	String _name;
	public SyncDecl(String name)
	{
		super();
		this._name = name;
	}
	
	public void write(Appendable sb, VarNameGenerator generator, ConstMapper constMap) throws IOException, EvaluationFailedException {
		sb.append("\npublic static ").append(Program.getSyncFactoryFullName()).append(" ").append(Program.syncNameToMethodName(_name)).append("(");
		Map<String, Const> _argMap = new HashMap<String, Const>();
		Iterator<Pair<String, Const>> it = _paraMap.iterator();
		if (it.hasNext()) {
			String paraName = it.next().getValue1();
			sb.append("Object ").append(paraName);
			_argMap.put(paraName, new JavaVarRef(paraName));
		}
		while(it.hasNext()) {
			String paraName = it.next().getValue1();
			sb.append(", ").append("Object ").append(paraName);
			_argMap.put(paraName, new JavaVarRef(paraName));
		}
		sb.append("){\n");
		String factoryVar = _body.writeInitializeFactory(sb, generator, new TwoMapperDelegation(new MapDelegation(_argMap), constMap));
		sb.append("\nreturn ").append(factoryVar).append(";\n}\n");
	}
	public Iterator<Pair<String, Const>> iterator()
	{
		return _paraMap.iterator();
	}
	public SyncBody getBody()
	{
		return _body;
	}

}
